var myChart;
var domCode = document.getElementById('sidebar-code');
var domGraphic = document.getElementById('graphic');
var domMain = document.getElementById('main');
var domMessage = document.getElementById('wrong-message');
var iconResize = document.getElementById('icon-resize');
var needRefresh = false;

var enVersion = location.hash.indexOf('-en') != -1;
var hash = location.hash.replace('-en', '');
hash = hash.replace('#', '') || (needMap() ? 'sakura' : 'sakura');
hash += enVersion ? '-en' : '';

var curTheme;

function requireCallback(ec, defaultTheme) {
    curTheme = {};
    echarts = ec;
    refresh();
    window.onresize = myChart.resize;
}
function tooltipClickEvent(argument) {
    // console.log(argument.innerText.split('\n')[1].split(' :')[0])
    window.location.href = 'http://www.wanda.cn/wandachanye/commercial/';
}

function selectChange(value) {
    var theme = value;
    myChart.showLoading();
    $(themeSelector).val(theme);
    if (theme != 'default') {
        window.location.hash = value + (enVersion ? '-en' : '');
        console.log(theme)
        require(['echarts/theme/sakura'], function(tarTheme) {
            curTheme = tarTheme;
            setTimeout(refreshTheme, 500);
        })
    } else {
        window.location.hash = enVersion ? '-en' : '';
        curTheme = {};
        setTimeout(refreshTheme, 500);
    }
}

function refreshTheme() {
    myChart.hideLoading();
    myChart.setTheme(curTheme);
}


function autoResize() {
    if ($(iconResize).hasClass('glyphicon-resize-full')) {
        focusCode();
        iconResize.className = 'glyphicon glyphicon-resize-small';
    } else {
        focusGraphic();
        iconResize.className = 'glyphicon glyphicon-resize-full';
    }
}

function focusCode() {
    domCode.className = 'col-md-8 ani';
    domGraphic.className = 'col-md-4 ani';
}

function focusGraphic() {
    domCode.className = 'col-md-4 ani';
    domGraphic.className = 'col-md-8 ani';
    if (needRefresh) {
        myChart.showLoading();
        setTimeout(refresh, 1000);
    }
}


var echartsOpts = {
    title: {
        text: '万达广场分布',
        subtext: '',
        show:false,
        x: 'center'
    },
    tooltip: {
        trigger: 'item'
    },
    legend: {
        show:false,
        orient: 'vertical',
        x: 'left',
        data: ['已开业万达广场']
    },
    dataRange: {
        min: 1,
        max: 20,
        splitNumber:19,
        show:false,
        color :['#1e90ff','#f0ffff']
        // x: 'left',
        // y: 'bottom',
        // text:['高','低'],           // 文本，默认为数值文本
        // calculable : true
    },
    // toolbox: {
    //     show: true,
    //     orient : 'vertical',
    //     x: 'right',
    //     y: 'center',
    //     feature : {
    //         mark : {show: true},
    //         dataView : {show: true, readOnly: false},
    //         restore : {show: true},
    //         saveAsImage : {show: true}
    //     }
    // },
    // roamController: {
    //     show: false,
    //     x: 'right',
    //     mapTypeControl: {
    //         'china': true
    //     }
    // },
    series: [{
        name: '已开业万达广场',
        type: 'map',
        mapType: 'china',
        roam: false,
        itemStyle: {
            normal: { label: { show: true } },
            emphasis: { label: { show: true } }
        },
        data: [
           { name: '北京', value: 7 },{ name: '天津', value: 2 },{ name: '上海', value: 8 },{ name: '重庆', value: 6 },{ name: '黑龙江', value: 8 },{ name: '吉林', value: 7 },{ name: '辽宁', value: 17 },{ name: '内蒙古', value: 7 },{ name: '河北', value: 3 },{ name: '河南', value: 10 },{ name: '山东', value: 15 },{ name: '山西', value: 3 },{ name: '陕西', value: 7 },{ name: '宁夏', value: 3 },{ name: '甘肃', value: 2 },{ name: '青海', value: 1 },{ name: '江苏', value: 26 },{ name: '安徽', value: 13 },{ name: '湖北', value: 12 },{ name: '浙江', value: 17 },{ name: '福建', value: 13 },{ name: '江西', value: 8 },{ name: '湖南', value: 5 },{ name: '四川', value: 17 },{ name: '广东', value: 17 },{ name: '广西', value: 9 },{ name: '云南', value: 3 },{ name: '海南', value: 1 },{ name: '新疆', value: 2 },{ name: '贵州', value: 2 },
        ]
    }]
};

function refresh(isBtnRefresh) {
    if (isBtnRefresh) {
        needRefresh = true;
        focusGraphic();
        return;
    }
    needRefresh = false;
    if (myChart && myChart.dispose) {
        myChart.dispose();
    }
    console.log(domMain, curTheme)
    myChart = echarts.init(domMain, curTheme);
    window.onresize = myChart.resize;
    myChart.setOption(echartsOpts, true)
    require(['echarts/theme/sakura'], function(tarTheme){
        curTheme = tarTheme;
        setTimeout(refreshTheme, 500);
    })
    myChart.on('click', function (params) {
        var city = params.name;
        console.log(city); 
        window.location.href = 'http://www.wanda.cn/wandachanye/commercial/';
    });

}
$('.echarts-tooltip').on('click', function (params) {
    console.log(this)
})

function needMap() {
    var href = location.href;
    return href.indexOf('map') != -1 || href.indexOf('mix3') != -1 || href.indexOf('mix5') != -1 || href.indexOf('dataRange') != -1;

}

var echarts;
var developMode = false;

if (developMode) {
    window.esl = null;
    window.define = null;
    window.require = null;
    (function() {
        var script = document.createElement('script');
        script.async = true;

        var pathname = location.pathname;

        var pathSegs = pathname.slice(pathname.indexOf('doc')).split('/');
        var pathLevelArr = new Array(pathSegs.length - 1);
        script.src = pathLevelArr.join('../') + 'asset/js/esl/esl.js';
        if (script.readyState) {
            script.onreadystatechange = fireLoad;
        } else {
            script.onload = fireLoad;
        }
        (document.getElementsByTagName('head')[0] || document.body).appendChild(script);

        function fireLoad() {
            script.onload = script.onreadystatechange = null;
            setTimeout(loadedListener, 100);
        }

        function loadedListener() {
            // for develop
            require.config({
                packages: [{
                    name: 'echarts',
                    location: '../../src',
                    main: 'echarts'
                }, {
                    name: 'zrender',
                    // location: 'http://ecomfe.github.io/zrender/src',
                    location: '../../../zrender/src',
                    main: 'zrender'
                }]
            });
            launchExample();
        }
    })();
} else {
    // for echarts online home page
    require.config({
        paths: {
            echarts: JS_PATH+'/plug/echarts/www/js'
        }
    });
    launchExample();
}

var isExampleLaunched;

function launchExample() {
    if (isExampleLaunched) {
        return;
    }

    // 按需加载
    isExampleLaunched = 1;
    require(
        [
            'echarts',
            'echarts/theme/sakura',
            'echarts/chart/line',
            'echarts/chart/bar',
            'echarts/chart/scatter',
            'echarts/chart/k',
            'echarts/chart/pie',
            'echarts/chart/radar',
            'echarts/chart/force',
            'echarts/chart/chord',
            'echarts/chart/gauge',
            'echarts/chart/funnel',
            'echarts/chart/eventRiver',
            'echarts/chart/venn',
            'echarts/chart/treemap',
            'echarts/chart/tree',
            'echarts/chart/wordCloud',
            'echarts/chart/heatmap',
            'echarts/chart/map' 
        ],
        requireCallback
    );
}
